package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.BabyAllResponse;

import java.util.ArrayList;

public class BabyAllAdapter extends RecyclerView.Adapter<BabyAllViewHolder> implements BabyAllViewHolder.OnClickHeadListener {

    public interface OnClickItemListener {
        void onClickItem(BabyAllResponse babyAllResponse);
    }

    private OnClickItemListener onClickItemListener;

    private ArrayList<BabyAllResponse> mList;

    private Context context;

    public BabyAllAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<BabyAllResponse>();
    }

    @Override
    public BabyAllViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_baby_all, parent, false);
        BabyAllViewHolder viewHolder = new BabyAllViewHolder(view);
        viewHolder.setOnClickHeadListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(BabyAllViewHolder holder, int position) {
        holder.setImageHeadUrl(mList.get(position).getPath());
        if (mList.get(position).getType() == 1) {
            holder.setImageCreate(true);
        } else {
            holder.setImageCreate(false);
        }
        switch (mList.get(position).getAuth().getRole()) {
            case 0:
                holder.setImageAdmin(false, null);
                break;
            case 1:
                holder.setImageAdmin(true, context.getResources().getDrawable(R.drawable.ic_admin));
                break;
            case 2:
                holder.setImageAdmin(true, context.getResources().getDrawable(R.drawable.ic_supper));
                break;
        }

        holder.setName(mList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        if (mList == null) {
            return 0;
        }
        return mList.size();
    }

    @Override
    public void onClickHead(BabyAllViewHolder holder) {
        int position = holder.getLayoutPosition();
        onClickItemListener.onClickItem(mList.get(position));
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public void clear() {
        mList.clear();
    }

    public void add(BabyAllResponse response) {
        mList.add(response);
    }

}
